package cn.antia.db;

/**
 * User: Jair Shaw
 * Date: 2008-10-10
 * Time: 11:33:48
 */
public class PostgreSQLGene extends AbstractSQLGene{
    
    public String composeSqlPage(String sql, String orderField, boolean isAsc, int page, int pageSize) {
        int start = page * pageSize;
        int end = start + pageSize;
        return composeSql(sql, orderField, isAsc, start, end);
    }
    
    /**
     * 如果给出了一个 LIMIT 计数，那么将返回不超过该数字的行
     * OFFSET 指明在开始返回行之前忽略多少行。
     * @param sql sql
     * @param orderField orderField
     * @param isAsc isAsc
     * @param start start
     * @param end end
     * @return sql
     */
    public String composeSql(String sql, String orderField, boolean isAsc, int start, int end) {
        sql = cutOrderBySql(sql);
        sql = sql + " ORDER BY " + orderField + (isAsc ? "ASC " : "DESC ");
        sql = sql + " LIMIT " + end + " OFFSET " + start;
        return sql;
    }
}
